package games.chasingdodo;

import games.chasingdodo.GameStateChasingDodo.OnGameStateUpdateListener;

import java.io.IOException;

import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.font.FontManager;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;

public class WindowLayer implements OnGameStateUpdateListener {

	private static final int POSITION_CORN_X = 165;
	
	/**
	 * Resouces
	 */
	private float camera_width;
	private float camera_height; 
	private ITextureRegion textureCorn;
	private Font fontBaconFarm, fontBaconFarmBigger;
	private Sprite spriteCorn;

	/**
	 * Rope
	 */
	private ITextureRegion textureRope;
	private Sprite spriteRope;
	private MoveModifier moveModifier;
	private int defaultPosition;

	/**
	 * Pot
	 */
	private ITextureRegion texturePotFront;
	private ITextureRegion texturePotBack;
	private Sprite spritePotFront;
	private Sprite spritePotBack;

	/**
	 * Texts
	 */
	private Text textCorn;
	private Text textStartingCounter;
	private LoopEntityModifier loopModifier;

	public WindowLayer(int camera_width, int camera_height) {
		this.camera_width = camera_width;
		this.camera_height = camera_height;
	}

	// ************************************* ANDENGINE *************************************

	public void onCreateResources(TextureManager textureManager, FontManager fontMaganer, AssetManager assetManager, Context context) throws IOException {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("imagenes/");

		BitmapTextureAtlas atlasCorn = new BitmapTextureAtlas(textureManager, 113, 70);
		textureCorn = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasCorn, context, "corn.png", 0, 0);
		atlasCorn.load();

		// Rope
		BitmapTextureAtlas atlasRope = new BitmapTextureAtlas(textureManager, 115, 360);
		textureRope = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasRope, context, "dodoSoga.png", 0, 0);
		atlasRope.load();

		// Pot
		BitmapTextureAtlas atlasPotFront = new BitmapTextureAtlas(textureManager, 115, 360);
		texturePotFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasPotFront, context, "potFront.png", 0, 0);
		atlasPotFront.load();
		BitmapTextureAtlas atlasPotBack = new BitmapTextureAtlas(textureManager, 115, 360);
		texturePotBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasPotBack, context, "potBack.png", 0, 0);
		atlasPotBack.load();

		// Corn next
		FontFactory.setAssetBasePath("fonts/");
		final ITexture fontTexture = new BitmapTextureAtlas(textureManager,256, 256, TextureOptions.BILINEAR);
		fontBaconFarm = FontFactory.createFromAsset(fontMaganer, fontTexture, assetManager, "BaconFarm.ttf", 80, true, Color.WHITE);
		fontBaconFarm.load();

		// Bigger text
		final ITexture fontTextureBigger = new BitmapTextureAtlas(textureManager,512, 512, TextureOptions.BILINEAR);
		fontBaconFarmBigger = FontFactory.createFromAsset(fontMaganer, fontTextureBigger, assetManager, "BaconFarm.ttf", 100, true, Color.DKGRAY);
		fontBaconFarmBigger.load();
	}

	public void onCreateScene(VertexBufferObjectManager vertexBufferObjectManager, Scene scene, int centerX, int centerY) {

		///TEXT CORN///		
		spriteCorn = new Sprite(POSITION_CORN_X, camera_height-50, textureCorn,vertexBufferObjectManager);
		scene.attachChild(spriteCorn);

		textCorn = new Text(spriteCorn.getX() + spriteCorn.getWidth() + 10, spriteCorn.getY(), fontBaconFarm, ": 0123456789",vertexBufferObjectManager);
		textCorn.setText("" + 0);

		scene.attachChild(textCorn);

		// Pot back
		spritePotBack = new Sprite(50,camera_height, texturePotBack,vertexBufferObjectManager);
		spritePotBack.setY(camera_height-spritePotBack.getHeight()/2);
		scene.attachChild(spritePotBack);

		///Rope///
		defaultPosition = (int) (camera_height*1.125) - 30;
		spriteRope = new Sprite(50,defaultPosition, textureRope,vertexBufferObjectManager);
		scene.attachChild(spriteRope);

		// Pot front
		spritePotFront = new Sprite(50,camera_height, texturePotFront,vertexBufferObjectManager);
		spritePotFront.setY(camera_height-spritePotFront.getHeight()/2);
		scene.attachChild(spritePotFront);

		// Beginning text
		textStartingCounter = new Text(centerX, centerY, fontBaconFarmBigger, "012345abcdefghijklmnopqrstuvwxyz!",vertexBufferObjectManager);
		textStartingCounter.setText("");
		scene.attachChild(textStartingCounter);
	}

	public void setCorn(int corn) {		
		textCorn.setText("" + corn);
	}

	public void setLife(int life){
		float percent = ((float)(GameStateChasingDodo.MAX_LIFE - life))/((float)GameStateChasingDodo.MAX_LIFE);
		if(moveModifier != null)
			spriteRope.unregisterEntityModifier(moveModifier);
		moveModifier = new MoveModifier(0.4f, spriteRope.getX(), spriteRope.getY(), spriteRope.getX(), defaultPosition - percent*camera_height/3);
		spriteRope.registerEntityModifier(moveModifier);
//		spriteRope.setY(defaultPosition - percent*camera_height/3);
	}

	// ************************************* ANDENGINE *************************************

	// ******************************** BEGINNING ANIMATION ********************************

	public void countDownAnimation(final int time, final boolean isCreator, final RoomManagerDodoGame roomMananager) {
		if(loopModifier == null) {
			loopModifier = new LoopEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.5f, 0.5f, 2f), new ScaleModifier(0.5f, 2f, 0.5f)));
			textStartingCounter.registerEntityModifier(loopModifier);
			new Thread(new Runnable() {

				@Override
				public void run() {
					int actualTime = time;
					while(actualTime > 0) {
						if(isCreator) {
							roomMananager.sendBeginningTime(actualTime);
							try {Thread.sleep(200);} catch (InterruptedException e) {}
						}
						textStartingCounter.setText(""+actualTime);
						try {Thread.sleep(799);} catch (InterruptedException e) {}
						actualTime --;
					}
					textStartingCounter.setText("GO!");
					try {Thread.sleep(999);} catch (InterruptedException e) {}
					textStartingCounter.unregisterEntityModifier(loopModifier);
					textStartingCounter.registerEntityModifier(new ScaleModifier(1f, 1f, 1f));
					textStartingCounter.setVisible(false);
				}
			}).start();
		}
	}

	// ******************************** BEGINNING ANIMATION ********************************

	// ******************************** GAME STATE LISTENER ********************************

	@Override
	public void onLifeChanged(int life) {
		setLife(life);	
	}

	@Override
	public void onCornChanged(int corn) {
		setCorn(corn);
	}

	@Override
	public void onBeginGame(boolean gameMode) {}

	@Override
	public void onChangePlayingMode(boolean gameMode, int corn) {
		textStartingCounter.setText("Game mode will change");
		textStartingCounter.setVisible(true);
	}

	@Override
	public void onFinishGame(boolean gameMode, int myScore, int enemyScore) {
		textStartingCounter.setText("My score: "+myScore+" / Enemy score: "+enemyScore);
		textStartingCounter.setVisible(true);
	}

	@Override
	public void onReplay(boolean gameMode) {}

	@Override
	public void onLostConnection() {}

	@Override
	public void onGameStopedByOther() {}

	// ******************************** GAME STATE LISTENER ********************************

}
